﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using SqlCon;
using System.Collections;

namespace yfxt
{
    public partial class ms_qxfy : Form
    {

        int i = 0;//发药提交标志
        public ms_qxfy()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (dataGridView1.Rows.Count == 0)
            {

                MessageBox.Show("你还没有输入处方号码或者是发票号码，请输入");

                return;
            }

            string s = "select @fybz=fybz from ms_cf01 where cfhm=@cfhm";

            SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);

            if (conn.State != ConnectionState.Open) { conn.Open(); }


            SqlCommand cmd_fybz = new SqlCommand(s, conn);

            cmd_fybz.Parameters.AddWithValue("@cfhm", dataGridView1.CurrentRow.Cells["cfhm"].Value.ToString());




            SqlParameter sp_02;

            SqlCommand cmdUpdate = new SqlCommand();

            SqlCommand cmd_kcsl = new SqlCommand();

            if (i == 0)
            {

                for (int rowcount = 0; rowcount < dataGridView2.Rows.Count; rowcount++)
                {


                    string StrSQL = "select @ypsl=ypsl from ms_cf01,ms_cf02,yk_typk,yk_ypcd,yk_cddz " +
                                    "  where ms_cf01.cfsb=ms_cf02.cfsb and yk_typk.ypxh=ms_cf02.ypxh " +
                                    "  and yk_typk.ypxh=yk_ypcd.ypxh and yk_cddz.ypcd=yk_ypcd.ypcd and yk_typk.ypxh="
                                      + dataGridView2.Rows[rowcount].Cells["ypxh"].Value.ToString() +
                                      " group by ypsl";

                    SqlCommand cmdSelect = new SqlCommand(StrSQL, conn);


                    sp_02 = cmdSelect.Parameters.Add("@ypsl", SqlDbType.Decimal, 20);

                    sp_02.Direction = ParameterDirection.Output;

                    cmdSelect.ExecuteNonQuery();

                    cmdSelect.Parameters.Clear();//清除参数 避免输出参数重复



                    string StrUpdate = "update yf_kcmx set ypsl=ypsl+" + sp_02.Value.ToString() + " where yf_kcmx.ypxh="
                                         + dataGridView2.Rows[rowcount].Cells["ypxh"].Value.ToString() + "";

                    cmdUpdate.Connection = conn;
                    cmdUpdate.CommandText = StrUpdate;

                    cmdUpdate.ExecuteNonQuery();


                }

                i = 1;

                conn.Close();

                DataTable dt_01 = (DataTable)dataGridView1.DataSource;
                dt_01.Rows.Clear();
                dataGridView1.DataSource = dt_01;


                DataTable dt_02 = (DataTable)dataGridView2.DataSource;
                dt_02.Rows.Clear();
                dataGridView2.DataSource = dt_02;


                MessageBox.Show("提交完毕");

            }
            else
            {

                MessageBox.Show("已经提交完毕，不需重复提交");

            }

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void ms_cqfy_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);

            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }

            string str = "select cfhm,brxm from ms_cf01 where cfhm=@cfhm";

            SqlCommand cmd = new SqlCommand(str, conn);

            cmd.Parameters.AddWithValue("@cfhm", "0");

            cmd.ExecuteNonQuery();

            SqlDataAdapter da = new SqlDataAdapter();

            da.SelectCommand = cmd;


            DataTable dt = new DataTable();

            da.Fill(dt);

            dataGridView1.DataSource = dt;

            dataGridView1.Columns[0].HeaderCell.Value = "处方号码";

            dataGridView1.Columns[1].HeaderCell.Value = "病人姓名";





            //显示病人姓名内容
            string str_x = "select @brxm=ms_cf01.brxm,@ygxm=ygxm,@kfrq=kfrq,@sfrq=sfrq  from ms_cf01,ms_cf02,ms_mzxx,gy_ygdm " +
                           " where ms_cf01.cfsb=ms_cf02.cfsb and ms_cf01.fphm=ms_mzxx.fphm and ms_cf01.ysdm=gy_ygdm.ygdm" +
                           " and ms_cf01.cfhm=@cfhm";

            SqlParameter sp_01;

            SqlParameter sp_02;

            SqlParameter sp_03;

            SqlParameter sp_04;

            SqlCommand cmd_01 = new SqlCommand(str_x, conn);

            sp_01 = cmd_01.Parameters.Add("@brxm", SqlDbType.NVarChar, 40);

            sp_02 = cmd_01.Parameters.Add("@ygxm", SqlDbType.NVarChar, 40);

            sp_03 = cmd_01.Parameters.Add("@kfrq", SqlDbType.DateTime);

            sp_04 = cmd_01.Parameters.Add("@sfrq", SqlDbType.DateTime);

            if (dataGridView1.Rows.Count == 0) { return; }

            cmd_01.Parameters.AddWithValue("@cfhm", dataGridView1.CurrentRow.Cells[0].Value.ToString());

            sp_01.Direction = ParameterDirection.Output;
            sp_02.Direction = ParameterDirection.Output;
            sp_03.Direction = ParameterDirection.Output;
            sp_04.Direction = ParameterDirection.Output;


            cmd_01.ExecuteNonQuery();

            tb_brxm.Text = sp_01.Value.ToString();

            tb_yis.Text = sp_02.Value.ToString();

            tb_kfrq.Text = sp_03.Value.ToString();

            tb_fyrq.Text = sp_04.Value.ToString();

            tb_yaos.Text = sp_02.Value.ToString();




        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb_fp_cf_KeyUp(object sender, KeyEventArgs e)
        {
        }

        private void tb_fp_cf_KeyDown(object sender, KeyEventArgs e)
        {
            string str;



            SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);



            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }

            if (tb_fp_cf.Text.Length == 2)
            {
                str = "select cfhm,brxm from ms_cf01 where cfhm=@cfhm";

                SqlCommand cmd = new SqlCommand(str, conn);

                cmd.Parameters.AddWithValue("@cfhm", tb_fp_cf.Text.ToString());

                cmd.ExecuteNonQuery();

                SqlDataAdapter da = new SqlDataAdapter();

                da.SelectCommand = cmd;


                DataTable dt = new DataTable();

                da.Fill(dt);

                dataGridView1.DataSource = dt;


                dataGridView1.Columns[0].HeaderCell.Value = "处方号码";

                dataGridView1.Columns[1].HeaderCell.Value = "病人姓名";
            }
            else
            {
                str = "select cfhm,brxm from ms_cf01 where fphm=@fphm";

                SqlCommand cmd = new SqlCommand(str, conn);

                cmd.Parameters.AddWithValue("@fphm", tb_fp_cf.Text.Trim().ToString());

                cmd.ExecuteNonQuery();

                SqlDataAdapter da = new SqlDataAdapter();

                da.SelectCommand = cmd;


                DataTable dt = new DataTable();

                da.Fill(dt);

                dataGridView1.DataSource = dt;

                dataGridView1.Columns[0].HeaderCell.Value = "处方号码";

                dataGridView1.Columns[1].HeaderCell.Value = "病人姓名";

            }


        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0) { return; }
            string str = dataGridView1.CurrentRow.Cells[0].Value.ToString();



            string StrSQL = "select yk_typk.ypxh as ypxh,ypmc,ypgg,ypdw,cdmc,ms_cf02.ypdj as ypdj,ms_cf02.ypsl as ypsl from ms_cf01,ms_cf02,yk_typk,yk_ypcd,yk_cddz "+
                            " where ms_cf01.cfsb=ms_cf02.cfsb and yk_typk.ypxh=ms_cf02.ypxh and yk_typk.ypxh=yk_ypcd.ypxh "+
                            " and yk_cddz.ypcd=yk_ypcd.ypcd and ms_cf01.cfhm=@cfhm "+
                            " group by yk_typk.ypxh,ypmc,ypgg,ypdw,cdmc,ms_cf02.ypdj,ypsl";


            SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);

            SqlDataAdapter da = new SqlDataAdapter();

            SqlCommand cmd = new SqlCommand(StrSQL, conn);

            cmd.Parameters.AddWithValue("@cfhm", str);

            da.SelectCommand = cmd;

            DataTable dt = new DataTable();

            da.Fill(dt);

            dataGridView2.DataSource = dt;

            dataGridView2.Columns["ypxh"].HeaderCell.Value = "药品序号";
            dataGridView2.Columns["ypmc"].HeaderCell.Value = "药品名称";
            dataGridView2.Columns["ypgg"].HeaderCell.Value = "药品规格";
            dataGridView2.Columns["ypdw"].HeaderCell.Value = "药品单位";
            dataGridView2.Columns["cdmc"].HeaderCell.Value = "药品产地";
            dataGridView2.Columns["ypdj"].HeaderCell.Value = "药品单价";
            dataGridView2.Columns["ypsl"].HeaderCell.Value = "药品数量";




            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }

            string str_x = "select @brxm=ms_cf01.brxm,@ygxm=ygxm,@kfrq=kfrq,@sfrq=sfrq  from ms_cf01,ms_cf02,ms_mzxx,gy_ygdm " +
                           " where ms_cf01.cfsb=ms_cf02.cfsb and ms_cf01.fphm=ms_mzxx.fphm and ms_cf01.ysdm=gy_ygdm.ygdm" +
                           " and ms_cf01.cfhm=@cfhm";

            SqlParameter sp_01;

            SqlParameter sp_02;

            SqlParameter sp_03;

            SqlParameter sp_04;

            SqlCommand cmd_01 = new SqlCommand(str_x, conn);

            sp_01 = cmd_01.Parameters.Add("@brxm", SqlDbType.NVarChar, 40);

            sp_02 = cmd_01.Parameters.Add("@ygxm", SqlDbType.NVarChar, 40);

            sp_03 = cmd_01.Parameters.Add("@kfrq", SqlDbType.DateTime);

            sp_04 = cmd_01.Parameters.Add("@sfrq", SqlDbType.DateTime);


            cmd_01.Parameters.AddWithValue("@cfhm", dataGridView1.CurrentRow.Cells[0].Value.ToString());

            sp_01.Direction = ParameterDirection.Output;
            sp_02.Direction = ParameterDirection.Output;
            sp_03.Direction = ParameterDirection.Output;
            sp_04.Direction = ParameterDirection.Output;


            cmd_01.ExecuteNonQuery();

            tb_brxm.Text = sp_01.Value.ToString();

            tb_yis.Text = sp_02.Value.ToString();

            tb_kfrq.Text = sp_03.Value.ToString();

            tb_fyrq.Text = sp_04.Value.ToString();

            tb_yaos.Text = sp_02.Value.ToString();

        }

        private void btn_tc_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btn_sx_Click(object sender, EventArgs e)
        {


            SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);

            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }

            string str_x = "select @brxm=ms_cf01.brxm,@ygxm=ygxm,@kfrq=kfrq,@sfrq=sfrq  from ms_cf01,ms_cf02,ms_mzxx,gy_ygdm "+
                           " where ms_cf01.cfsb=ms_cf02.cfsb and ms_cf01.fphm=ms_mzxx.fphm and ms_cf01.ysdm=gy_ygdm.ygdm"+
                           " and ms_cf01.cfhm=@cfhm";

            SqlParameter sp_01;

            SqlParameter sp_02;

            SqlParameter sp_03;

            SqlParameter sp_04;

            SqlCommand cmd_01 = new SqlCommand(str_x, conn);

            sp_01 = cmd_01.Parameters.Add("@brxm", SqlDbType.NVarChar, 40);

            sp_02 = cmd_01.Parameters.Add("@ygxm", SqlDbType.NVarChar, 40);

            sp_03 = cmd_01.Parameters.Add("@kfrq", SqlDbType.DateTime);

            sp_04 = cmd_01.Parameters.Add("@sfrq", SqlDbType.DateTime);

            if (dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show("当前没有退药处方");
                return;
            }

            cmd_01.Parameters.AddWithValue("@cfhm", dataGridView1.CurrentRow.Cells[0].Value.ToString());

            sp_01.Direction = ParameterDirection.Output;
            sp_02.Direction = ParameterDirection.Output;
            sp_03.Direction = ParameterDirection.Output;
            sp_04.Direction = ParameterDirection.Output;


            cmd_01.ExecuteNonQuery();

            tb_brxm.Text = sp_01.Value.ToString();

            tb_yis.Text = sp_02.Value.ToString();

            tb_kfrq.Text = sp_03.Value.ToString();

            tb_fyrq.Text = sp_04.Value.ToString();

            tb_yaos.Text = sp_02.Value.ToString();



        }

        private void btn_qr_Click(object sender, EventArgs e)
        {

            if (dataGridView1.Rows.Count == 0)
            {

                MessageBox.Show("你还没有输入处方号码或者是发票号码，请输入");

                return;
            }

            string s = "select @fybz=fybz from ms_cf01 where cfhm=@cfhm";

            SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);

            if (conn.State != ConnectionState.Open) { conn.Open(); }


            SqlCommand cmd_fybz = new SqlCommand(s, conn);

            cmd_fybz.Parameters.AddWithValue("@cfhm", dataGridView1.CurrentRow.Cells["cfhm"].Value.ToString());




            SqlParameter sp_02;

            SqlCommand cmdUpdate = new SqlCommand();

            SqlCommand cmd_kcsl = new SqlCommand();

            if (i == 0)
            {

                for (int rowcount = 0; rowcount < dataGridView2.Rows.Count; rowcount++)
                {


                    string StrSQL = "select @ypsl=ypsl from ms_cf01,ms_cf02,yk_typk,yk_ypcd,yk_cddz "+
                                   "  where ms_cf01.cfsb=ms_cf02.cfsb and yk_typk.ypxh=ms_cf02.ypxh "+
                                   "  and yk_typk.ypxh=yk_ypcd.ypxh and yk_cddz.ypcd=yk_ypcd.ypcd and yk_typk.ypxh="
                                     + dataGridView2.Rows[rowcount].Cells["ypxh"].Value.ToString() +
                                     " group by ypsl";

                    SqlCommand cmdSelect = new SqlCommand(StrSQL, conn);


                    sp_02 = cmdSelect.Parameters.Add("@ypsl", SqlDbType.Decimal, 20);

                    sp_02.Direction = ParameterDirection.Output;

                    cmdSelect.ExecuteNonQuery();

                    cmdSelect.Parameters.Clear();//清除参数 避免输出参数重复



                    string StrUpdate = "update yf_kcmx set ypsl=ypsl+" + sp_02.Value.ToString() + " where yf_kcmx.ypxh="
                                         + dataGridView2.Rows[rowcount].Cells["ypxh"].Value.ToString() + "";

                    cmdUpdate.Connection = conn;
                    cmdUpdate.CommandText = StrUpdate;

                    cmdUpdate.ExecuteNonQuery();


                }

                i = 1;

                conn.Close();
                //清除页面数据
                DataTable dt_01 = (DataTable)dataGridView1.DataSource;
                dt_01.Rows.Clear();
                dataGridView1.DataSource = dt_01;


                DataTable dt_02 = (DataTable)dataGridView2.DataSource;
                dt_02.Rows.Clear();
                dataGridView2.DataSource = dt_02;


                MessageBox.Show("提交完毕");

            }
            else
            {

                MessageBox.Show("已经提交完毕，不需重复提交");

            }
            
        }

        private void ms_qxfy_Load(object sender, EventArgs e)
        {

        }
    }
}
